package adminLoginPack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ImmovableIndia.ConnectionPool;

public class ArticleAction extends Action {

	  ConnectionPool pool=null;
	  public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		  ResultSet rs = null;
		  Connection con=null;
		  Statement st = null;
		 
		  try{
			  	String methodName = request.getParameter("methodName");
			  
			  	String aarticlesNumbers = "";
			  	List optionsList123=new ArrayList();
			  	
			  	
			  	int isUpdate = 0;
			  
			  	con=getConnection();
				st=con.createStatement();
				
				ArrayList articleNumbersBasedOnServiceNumber=new ArrayList();
				
				

				if("gettingArticleNumebrsbasedOnServiceNumber".equals(methodName)){
					
					String seviceNumber = request.getParameter("selectedServiceOption");
					rs=st.executeQuery("select ArticlesNumber from articles where Service_no="+seviceNumber);

					while(rs.next()){
						articleNumbersBasedOnServiceNumber.add(rs.getString(1));
					}
					
					request.setAttribute("articleNumbersBasedOnServiceNumber", articleNumbersBasedOnServiceNumber);
					request.setAttribute("ServiveNumbersData", "ServiveNumbersData");
				
					response.getWriter().write(aarticlesNumbers);				
					
				}else if("gettingArticlesDetailsBasedOnNotificationsNumber".equals(methodName)){
					
					String selectedArticlesNumber = request.getParameter("selectedArticlesNumber");
					String selectedServiceNumber = request.getParameter("selectedServiceNumber");
					
					rs=st.executeQuery("select articlesSubjectMatter,date_of_article from articles where ServiceNum="+selectedServiceNumber+" and ArticlesNumber="+selectedArticlesNumber+"");
					
					while(rs.next()){
						request.setAttribute("articlesSubjectMatter", rs.getString(1));
						request.setAttribute("date_of_article", rs.getString(2));
					}
					
					request.setAttribute("ServiveNumbersData", "ArticlesNumberDetailsBasedOnServicenumberAndCircularDate");
					response.getWriter().write(aarticlesNumbers);
					
				}
				
				
		  }catch(Exception e){
			  e.printStackTrace();
		  }finally{
			  try {
					if(null != rs)
						pool.releaseResources(rs);
					System.out.println("Connection RS closed........--------------");
					if(null != st)
						pool.releaseResources(st);
					System.out.println("Connection ST closed........--------------");
					if(null != con)
					pool.releaseConnection(con);
					System.out.println("Connection con closed........--------------");
				} catch (Exception e) {
					e.printStackTrace();
				}
		  }
		  return mapping.findForward("circularnumbers");
	  }
	  public Connection getConnection() throws SQLException, Exception {
			pool=new ConnectionPool();
			return pool.getConnection();
	}
}
